package com.example.fastjson.parse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;
import com.example.fastjson.dto.UserInfoDTO;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class JsonParseDemo {
    public static void main(String[] args) {
        String text = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        JSONObject data = JSON.parseObject(text);
        System.out.println("Name: " + data.getString("name"));


        String list = "[{\"name\":\"John\",\"age\":30,\"city\":\"New York\"},{\"name\":\"Jane\",\"age\":28,\"city\":\"Chicago\"}]";
        // 将 JSON 字符串解析为 JSONArray 对象
        List<UserInfoDTO> userInfoDTOList = JSON.parseArray(list, UserInfoDTO.class);



        // 将 JSON 字符串解析为 Java 对象
        UserInfoDTO userInfoDTO = JSON.parseObject(text, UserInfoDTO.class);


        // 将Java对象序列化为JSON字符串
        String result = JSON.toJSONString(userInfoDTO, JSONWriter.Feature.PrettyFormat);

        System.out.println(result);
        String jsonArrayStr = "[{\"id\":3,\"name\":\"王五\",\"email\":\"wangwu@company.com\"},{\"id\":4,\"name\":\"赵六\",\"email\":\"zhaoliu@company.com\"}]";
        List<User> userList = JSON.parseArray(jsonArrayStr, User.class);

        List<User> userList1 = Arrays.asList(
                new User(1001L, "张三", "zhangsan@company.com", LocalDateTime.now()),
                new User(1002L, "李四", "lisi@company.com", LocalDateTime.now())
        );

        // ✅ 推荐：直接序列化 List，FastJSON2 自动识别泛型结构
        String json = JSON.toJSONString(userList);

        System.out.println("【标准序列化】用户列表转 JSON：");
        System.out.println(json);
        // 输出：[{"id":1001,"name":"张三","email":"zhangsan@company.com","createTime":"2025-10-15T11:00:00"}, ...]

        // ✅ 推荐：反序列化为 List<User>，使用 JSON.parseType() 指定泛型
        // 注意：不能直接写 JSON.parseObject(json, List<User>.class)，Java 泛型擦除导致失败
        List<User> parsedList = JSON.parseArray(json, User.class);


        System.out.println("\n【标准反序列化】JSON 转用户列表：");
        System.out.println("总人数：" + parsedList.size());
        parsedList.forEach(u -> System.out.println("用户：" + u.getName()));

        User user = new User(1001L, "张三", "zhangsan@company.com", LocalDateTime.now());
        // 序列化时忽略null值
        user.setEmail(null);
        String ignoreNullJson = JSON.toJSONString(user,
                JSONWriter.Feature.WriteNulls,  // 包含null值
                JSONWriter.Feature.PrettyFormat);
        System.out.println("\n【忽略null值】用户转 JSON：" + ignoreNullJson);


        String jsonStr = "{\"id\":2,\"name\":\"李四\",\"age\":30,\"createTime\":\"2024-01-15T10:30:00\",\"extraInfo\":{\"city\":\"上海\"}}";
        // 解析为泛型Map
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<>() {});


        String jsonString = JSON.toJSONString(map, JSONWriter.Feature.PrettyFormat);
        System.out.println("解析为Map: " + jsonString);

        String[] strArray = JSON.parseObject(json, String[].class);
        System.out.println("解析为数组: " + Arrays.toString(strArray));
    }

    @Data
    @AllArgsConstructor
    static class User {
        private Long id;
        private String name;
        private String email;
        private LocalDateTime createTime;
    }
}
